function createAnime(target) {
    return {
        target,
        AnimeLock: false,
        mainLine: Promise.resolve(),

        Loop(number) {
            return new Promise((resolve) => {
                this.loop = true;
                this.play();
                this.onLoop = () => {
                    number--;
                    if (number <= 0) {
                        this.stop();
                        this.onLoop = null;
                        resolve();
                    }
                };
            });
        },
        anime(animeOptions) {
            if (this.AnimeLock === true) return;
            this.AnimeLock = true;
            const animation = () =>
                new Promise((resolve) => {
                    window.anime({
                        ...animeOptions,
                        targets: this.target,
                        complete(anim) {
                            animeOptions.complete &&
                                animeOptions.complete(anim);
                            resolve();
                        },
                    });
                });
            this.mainLine = this.mainLine
                .then(() => animation())
                .then(() => {
                    this.AnimeLock = false;
                })
                .catch((e) => {
                    console.log("动画发生错误", e);
                });
            return this.mainLine;
        },
    };
}

export default function addAnime(sprite) {
    Object.assign(sprite, createAnime(sprite));
}
